var WebSocketServer = require('ws').Server,
wss = new WebSocketServer({ port: 3002 });//服务端口8181

var mongoose = require('mongoose'),
    DB_URL = 'mongodb://localhost:27017/test_log';

mongoose.connect(DB_URL, function(err) {
    if (err) {
        console.log(err)
        return
    }

    console.log('mongodb连接成功')
});

mongoose.connection.on('connected', function () {    
    console.log('Mongoose connection open to ' + DB_URL);  
});  

/**
 * 连接异常
 */
 mongoose.connection.on('error',function (err) {    
    console.log('Mongoose connection error: ' + err);  
});    
 
/**
 * 连接断开
 */
mongoose.connection.on('disconnected', function () {    
    console.log('Mongoose connection disconnected');  
});

wss.on('connection', function (ws) {
    console.log('服务端：客户端已连接');
    ws.on('message', function (message) {
        //打印客户端监听的消息
        console.log(message);
    });
});
